﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext;
using Coolite.Ext.Web;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    public void LoginChk(object sender,AjaxEventArgs e)
    {
        if (ComboBox2.SelectedIndex == -1)
        {
            Ext.MessageBox.Alert("错误提示", "请选择用户!").Show();
            return;
        }
        if (TfPassWd.Text.Trim() == "")
        {
            Ext.MessageBox.Alert("错误提示", "密码不能为空！").Show();
            return;
        }

        Query q = new Query(CxUserBase.Schema);
        q.AddWhere("uid", ComboBox2.SelectedItem.Value);
        q.AddWhere("passWD", TfPassWd.Text); 
        if(q.GetRecordCount()>0)
        {
            IDataReader dr = q.ExecuteReader();
            if(dr.Read())
            {
                string uID = dr["uid"].ToString();
                string username = dr["username"].ToString();
                string cDepID = dr["DEPID"].ToString();
                string cLevelID = dr["LevelID"].ToString();
                string status = dr["status"].ToString();
                string securityNum = dr["SecurityNum"].ToString();
                if (status != "1")
                {

                    Ext.MessageBox.Alert("错误提示", "该用户已禁用!").Show();
                    return;
                }
                Query q2 = new Query(CxUserLogin.Schema);
                q2.SetSelectList("intime");
                q2.AddWhere("uid", uID);
                q2.ORDER_BY("intime desc");
                if (q2.GetRecordCount() != 0)
                {
                    Cookies.addCookies("cIntime", q2.ExecuteScalar().ToString(), 0);
                }
                else { Cookies.addCookies("cIntime", "无", 0); }
                
                UserLogin.Add(uID, username, "登录成功");
                
                Cookies.addCookies("cUID",uID,0);
                Cookies.addCookies("cUsername",username,0);
                if (cDepID == "21")
                {
                    Cookies.addCookies("cDeptID", "1", 0);
                }
                else 
                {
                    Cookies.addCookies("cDeptID", cDepID, 0);
                } 
                Cookies.addCookies("cstatus", status, 0);
                
                Cookies.addCookies("cLevelID",cLevelID,0);
                Cookies.addCookies("cSecurityNum", securityNum,0);
                Cookies.addCookies("cMD5", Cookies.md5(username + "$" + securityNum + "$" + Cookies.CookiesKey), 0);
            }
            Response.Redirect("~/main.aspx");
        }
        else
        {
            UserLogin.Add(ComboBox2.SelectedItem.Value, ComboBox2.SelectedItem.Text, "登录失败：密码错误");
            Ext.MessageBox.Alert("错误提示", "用户不存在或用户名密码错误!").Show();
        }
        
    }

     
    [AjaxMethod]
    public void userBind()
    {
        string deptid = ComboBox1.SelectedItem.Value.ToString();
        Query q = new Query(CxUserBase.Schema);
         
        if (deptid == "7")
        {
            q.AddWhere("depid", 7).OR("depid", 11).OR("depid", 12).OR("depid", 13).OR("depid", 14);
        }
        if (deptid == "10")
        {
            q.AddWhere("depid", 10).OR("depid", 15).OR("depid", 16).OR("depid", 17).OR("depid", 18).OR("depid", 19).OR("depid", 20);
        }
        if (deptid != "7" && deptid!="10")
        {
            q.AddWhere("depid", deptid);
        }
        
        q.SetSelectList("username,uid");
        //Ext.MessageBox.Alert("错误提示", q.GetSql()).Show();
        Store2.DataSource = q.ExecuteDataSet();
        Store2.DataBind();
    }
    protected int getDeptId(int deptId)
    {
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        return deptId;
    }
}
